/**
 * 权限
 */
import { useUserStore } from '@/stores/user'
import { useRouter, useRoute } from 'vue-router'

/**
 * 登录判断（token是否存在）
 * 	使用如下：
import { onLoad } from '@dcloudio/uni-app'
import {useAuthCheck} from '@/common/auth'
onLoad(() => {
	useAuthCheck()
})
 */
export function useAuthCheck() {
	const userStore = useUserStore()
	const router = useRouter()
	const route = useRoute()

	const isAuth = !!userStore.token

	if (!isAuth) {
		const path = route.fullPath // 当前地址，如: /pages/detail?id=123
		userStore.backUrl = path

		// 提示
		uni.showToast({
			title: '请登录',
			icon: 'none',
			duration: 1500
		})

		// 延迟跳转
		setTimeout(() => {
			router.replace({ path: '/pages/auth/login' })
		}, 1000)
	}

	return isAuth
}

